package com.app.web.jst.goods;

import com.alibaba.fastjson.JSON;
import com.app.utils.base.http.ResultUtil;
import com.app.utils.base.j2se.Common;
import com.app.utils.base.j2se.Convert;
import com.app.utils.base.j2se.JsonUtil;
import com.app.utils.base.j2se.StringHandler;
import com.app.utils.jst.Data.GoodsDetail;
import com.app.utils.jst.Data.GoodsList;
import com.app.utils.jst.JstEnum;
import com.app.utils.jst.JstHandler;
import com.app.utils.jst.paramBean.GoodsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "聚水潭接口")
@RestController
@RequestMapping("jst/goods/")
public class JstGoodsController {

    @ApiOperation(value = "商品详情")
    @ApiImplicitParam(name = "id",value = "商品id",dataType = "String ",required=true)
    @RequestMapping("details")
    public String goods(String id){
        GoodsParam param = GoodsParam.builder().item_spuId(id).build();

        Map<String,Object> res = JstHandler.execute(JstEnum.Url.商品详情查询.getUri(), JsonUtil.toJson(param));

        if(Common.isEmpty(res.get("data"))){
            return ResultUtil.error(555,"暂无数据");
        }
        Map<String,Object> map = (Map<String, Object>) JsonUtil.jsonToBean(Convert.objToStr(res.get("data")), Map.class);
        return ResultUtil.success(GoodsDetail.convert(map));
    }

    /**
     * 项目搜索页
     * @param name  项目名称
     * @param page  分页
     * @return
     */
    @RequestMapping("list")
    @ApiOperation(value = "项目搜索页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "项目名称",dataType = "String ",required  = false),
            @ApiImplicitParam(name = "categoryName",value = "类目名称",dataType = "String ",required  = false),
            @ApiImplicitParam(name = "page",value = "分页",dataType = "int",required  = false)
    })
    public String goodsListSearch(@RequestParam(defaultValue = "") String name,
                                  @RequestParam(defaultValue = "") String categoryName,
                                  @RequestParam(defaultValue = "1") int page){
        if(StringHandler.isNotEmpty(categoryName) && categoryName.equals("全部")){
            categoryName = "";
        }
        GoodsParam param = GoodsParam.builder().page_num(page).page_size(20).item_name(Common.isNotEmpty(name)?name:categoryName).build();

        Map<String,Object> res = JstHandler.execute(JstEnum.Url.商品列表查询.getUri(), JsonUtil.toJson(param));

        if(Common.isEmpty(res.get("data"))){
            return ResultUtil.success(0,"暂无数据");
        }

        List<Object> list = (List<Object>) JsonUtil.jsonToBean(Convert.objToStr(res.get("data")), Map.class).get("list");
        List<GoodsList> goodsLists = list.stream().filter(x->{
            Map<String, Object> data = (Map<String, Object>) x;
            return StringHandler.yuanToFen(Convert.objToStr(data.get("min_sale_price")))>0
                    && StringHandler.yuanToFen(Convert.objToStr(data.get("min_sale_price")))>StringHandler.yuanToFen(Convert.objToStr(data.get("min_supply_price")));
        }).map(x-> GoodsList.convert((Map<String, Object>) x)).collect(Collectors.toList());

        return ResultUtil.success(JSON.toJSON(goodsLists));
    }

}
